//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.wcp.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.farm.authority.FarmAuthorityService;
import com.farm.authority.service.OutuserServiceInter;
import com.farm.authority.service.UserServiceInter;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.auth.exception.CheckCodeErrorException;
import com.farm.core.auth.exception.LoginUserNoExistException;
import com.farm.core.page.ViewMode;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.doc.tag.DefaultIndexPageTaget;
import com.farm.parameter.FarmParameterService;
import com.farm.resume.service.ResumebaseServiceInter;
import com.farm.wcp.log.WcpLog;
import com.farm.wcp.util.ThemesUtil;
import com.farm.web.WebUtils;

@RequestMapping({"/login"})
@Controller
public class LoginController extends WebUtils {
    private static final Logger log = Logger.getLogger(LoginController.class);
    @Resource
    private ResumebaseServiceInter resumeBaseServiceImpl;
    @Resource
    private OutuserServiceInter outUserServiceImpl;
    @Resource
    private UserServiceInter userServiceImpl;
    @Resource
    private FarmFileManagerInter farmFileManagerImpl;

    public LoginController() {
    }

    @RequestMapping({"/submit"})
    public ModelAndView loginCommit(String name, String password, String checkcode, HttpServletRequest request, HttpSession session) {
        try {
            name = this.checkcodeAndfindUser(checkcode, name, session);
            if (FarmAuthorityService.getInstance().isLegality(name, password)) {
                FarmAuthorityService.loginIntoSession(session, this.getCurrentIp(request), name);
                WcpLog.log.info(this.getCurrentUser(session).getName() + ":" + this.getCurrentUser(session).getLoginname() + "登录系统");
                return ViewMode.getInstance().returnRedirectUrl("/frame/index.do");
            } else {
                return ViewMode.getInstance().putAttr("message", "用户密码错误").returnModelAndView("frame/login");
            }
        } catch (LoginUserNoExistException var7) {
            log.info("当前用户不存在");
            return ViewMode.getInstance().putAttr("message", "当前用户不存在").returnModelAndView("frame/login");
        } catch (CheckCodeErrorException var8) {
            log.info(var8.getMessage());
            return ViewMode.getInstance().putAttr("message", var8.getMessage()).returnModelAndView("frame/login");
        }
    }

    @RequestMapping({"LoginUser_PassWordUpdata"})
    @ResponseBody
    public Object editPassword(HttpSession session, String checkcode, String passwordl, String passwordn1) {
        return ViewMode.getInstance().setError("禁用").returnObjMode();
    }

    @RequestMapping({"/webPage"})
    public ModelAndView login(HttpSession session, HttpServletRequest request) {
        String url = request.getHeader("Referer");
        session.setAttribute(FarmParameterService.getInstance().getParameter("farm.constant.session.key.from.url"), url);
        return ViewMode.getInstance().returnModelAndView(ThemesUtil.getThemePath() + "/login");
    }

    private String checkcodeAndfindUser(String checkcode, String name, HttpSession session) throws CheckCodeErrorException {
        if (FarmParameterService.getInstance().getParameter("config.sys.verifycode.able").equals("true")) {
            boolean ischeck = checkcode.toUpperCase().equals(session.getAttribute("verCode"));
            session.removeAttribute("verCode");
            if (!ischeck) {
                throw new CheckCodeErrorException("验证码错误");
            }
        }

        if (name == null) {
            return null;
        } else {
            String userid = this.resumeBaseServiceImpl.getUseridByEmailOrMobile(name);
            if (userid != null) {
                LoginUser user = FarmAuthorityService.getInstance().getUserById(userid);
                if (user != null) {
                    name = user.getLoginname();
                }
            }

            return name;
        }
    }

    @RequestMapping({"/websubmit"})
    public ModelAndView webLoginCommit(String checkcode, String name, String password, HttpServletRequest request, HttpSession session) {
        try {
            name = this.checkcodeAndfindUser(checkcode, name, session);
            if (FarmAuthorityService.getInstance().isLegality(name, password)) {
                FarmAuthorityService.loginIntoSession(session, this.getCurrentIp(request), name);
                String goUrl = this.getGoUrl(session, FarmParameterService.getInstance());
                WcpLog.log.info(this.getCurrentUser(session).getName() + ":" + this.getCurrentUser(session).getLoginname() + "登录系统");
                return ViewMode.getInstance().returnRedirectUrl(goUrl);
            } else {
                return ViewMode.getInstance().putAttr("loginname", name).setError("用户密码错误").returnModelAndView(ThemesUtil.getThemePath() + "/login");
            }
        } catch (LoginUserNoExistException var7) {
            log.info("当前用户不存在");
            return ViewMode.getInstance().putAttr("loginname", name).setError("当前用户不存在").returnModelAndView(ThemesUtil.getThemePath() + "/login");
        } catch (CheckCodeErrorException var8) {
            return ViewMode.getInstance().putAttr("loginname", name).setError("验证码错误").returnModelAndView(ThemesUtil.getThemePath() + "/login");
        }
    }

    @RequestMapping({"/webout"})
    public ModelAndView weblogOut(String name, HttpSession session) {
        this.clearCurrentUser(session);
        return ViewMode.getInstance().returnRedirectUrl("/" + DefaultIndexPageTaget.getDefaultIndexPage());
    }

    @RequestMapping({"/page"})
    public ModelAndView login(String name) {
        return ViewMode.getInstance().returnModelAndView("frame/login");
    }

    @RequestMapping({"/out"})
    public ModelAndView logOut(String name, HttpSession session) {
        this.clearCurrentUser(session);
        return ViewMode.getInstance().returnRedirectUrl("/login/page.do");
    }
}
